package com.housekeeping.housekeeping.serviceTest.controller.orders;/*
 * @author  pyh
 * @date  2021/1/22 0022 下午 3:44
 * @version 1.0
 * @since JDK1.8_241
 */

import com.housekeeping.housekeeping.advertisingmanagement.service.SearchService;
import com.housekeeping.housekeeping.order.entity.Order;
import com.housekeeping.housekeeping.order.service.OrderDetailServ;
import com.housekeeping.housekeeping.order.service.impl.OrderDetailServImpl;
import com.housekeeping.housekeeping.order.utils.PageUtils;
import com.housekeeping.housekeeping.order.utils.ResponseCode;
import com.housekeeping.housekeeping.order.utils.ResponseEntity;
import com.housekeeping.housekeeping.order.utils.ResponseUtils;
import com.housekeeping.housekeeping.serviceTest.dao.impl.OrderTestDaoImpl;
import com.housekeeping.housekeeping.serviceTest.entity.Orders;
import com.housekeeping.housekeeping.serviceTest.service.OrdersDetailService;
import com.housekeeping.housekeeping.serviceTest.service.SearchTestService;
import com.housekeeping.housekeeping.serviceTest.service.impl.OrdersDetailServImpl;
import com.housekeeping.housekeeping.serviceTest.service.impl.SearchTestServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/orders/searchId")
public class OrdersSearchById extends HttpServlet {
    private OrdersDetailService orderDetailServ = new OrdersDetailServImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PageUtils<List<Orders>> orders = new PageUtils<>();
        List<Orders> list = new ArrayList<>();
        ResponseEntity<PageUtils<List<Orders>>> entity = null;

        //调用业务逻辑
        try {
            Orders orderContent = orderDetailServ.getOrderContent(id);
            if (orderContent != null) {
                list.add(orderContent);         //为了配合前端的json分页解析，用数组包裹
                orders.setData(list);
                orders.setTotalCount(1);         //设置为1就好，因为根据订单号查询，结果是唯一的

                entity = ResponseEntity.success(orders);
            } else {
                entity = ResponseEntity.error(ResponseCode.ORDER_SEARCH_ERROR);
            }
        } catch (SQLException throwables) {
            entity = ResponseEntity.error(ResponseCode.ORDER_SEARCH_ERROR);
        }

        ResponseUtils.deliver(resp, entity);
    }
}
